/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.DataType;
import org.eclipse.uml2.uml.LiteralBoolean;
import org.eclipse.uml2.uml.LiteralInteger;
import org.eclipse.uml2.uml.LiteralString;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;
import com.cea.papyrus.core.ui.dialogs.PapyrusMessageDialog;

/**
 * This class handles properties default values editing.
 * 
 * @author Chokri Mraidha
 */
public class DefaultValueSection extends PropertyViewSection {


	/**
	 * 
	 */
	protected class MyFocusListener implements org.eclipse.swt.events.FocusListener {
		
		/**
		 * 
		 * 
		 * @param e 
		 */
		public void focusGained(FocusEvent e){
		}
		
		/**
		 * 
		 * 
		 * @param e 
		 */
		public void focusLost(FocusEvent e){
			if(getElement() instanceof Property) { 

				// if property was not a typed element, text was not editable. losing focus is not effective.
				if(((Property)getElement()).getType() == null) {
					return;
				}
				
				// Property type is Integer
				if(((Property)getElement()).getType().getName().equals("Integer")) {
					LiteralInteger val = UMLFactory.eINSTANCE.createLiteralInteger();
					try {
						val.setValue(Integer.parseInt(valueText.getText()));
					}
					catch (NumberFormatException nf) {
						PapyrusMessageDialog.display("The selected default value is not an integer", nf);
					}
					((Property)getElement()).setDefaultValue(val);
				}

				// Property type is String
				else if(((Property)getElement()).getType().getName().equals("String")) {
					LiteralString val = UMLFactory.eINSTANCE.createLiteralString();
					val.setValue(valueText.getText());
					((Property)getElement()).setDefaultValue(val);
				}

				// Property type is Boolean
				else if(((Property)getElement()).getType().getName().equals("Boolean")) {
					LiteralBoolean val = UMLFactory.eINSTANCE.createLiteralBoolean();
					val.setValue(Boolean.parseBoolean(valueText.getText()));
					((Property)getElement()).setDefaultValue(val);
				}

				// Property type is a datatype
				else if(((Property)getElement()).getType() instanceof DataType) {
					((Property)getElement()).setDefault(valueText.getText());
				}

			}
		} 
	};


	/**
	 * 
	 */
	protected MyFocusListener nameListener = new MyFocusListener();

	/**
	 * 
	 */
	private Text valueText;


	/**
	 * 
	 * 
	 * @param tabbedPropertySheetPage 
	 * @param parent 
	 * 
	 * @see org.eclipse.ui.views.properties.tabbed.ITabbedPropertySection#createControls(org.eclipse.swt.widgets.Composite,
	 * org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage)
	 */
	@Override
	public void createControls(Composite parent,
			TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);
		Composite composite = getWidgetFactory().createFlatFormComposite(parent);
		FormData data;

		valueText = getWidgetFactory().createText(composite, ""); //$NON-NLS-1$
		valueText.setEditable(true);
		valueText.addFocusListener(nameListener);
		data = new FormData();
		data.left = new FormAttachment(0, STANDARD_LABEL_WIDTH);
		data.right = new FormAttachment(50, 0);
		data.top = new FormAttachment(0, 0);
		valueText.setLayoutData(data);


		CLabel nameLabel = getWidgetFactory().createCLabel(composite, "Default V.:"); //$NON-NLS-1$
		nameLabel.setToolTipText("Default value");
		
		data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(valueText,
				-ITabbedPropertyConstants.HSPACE);
		data.top = new FormAttachment(valueText, 0, SWT.CENTER);
		nameLabel.setLayoutData(data);

	}



	/*
	 * @see org.eclipse.ui.views.properties.tabbed.view.ITabbedPropertySection#refresh()
	 */
	/**
	 * 
	 */
	@Override
	public void refresh() {
		if (!valueText.isDisposed()){
			valueText.removeFocusListener(nameListener);
			valueText.setText("");
			
			Property property = null;
			if(getElement() instanceof Property) {
				property = (Property)getElement();
			} else {
				return;
			}
			
			if(property.getType() == null) {
				valueText.setEditable(false);
				valueText.setText("--- Default value can not be set since property type is undefined --- ");
			} else {
				valueText.setEditable(true);
				if(property.getDefault() != null) {
					valueText.setText(((Property)getElement()).getDefault());
				}
			}
			valueText.addFocusListener(nameListener);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
	 */
	/**
	 * 
	 */
	public void dispose(){
		super.dispose();
		if(valueText!=null && !valueText.isDisposed())
			valueText.removeFocusListener(nameListener);
	}

}
